package com.stations;

import cn.hutool.core.util.StrUtil;
import com.github.jeffreyning.mybatisplus.conf.EnableMPP;
import com.stations.common.utils.SystemInfoUtils;
import com.stations.domain.SysConfig;
import com.stations.mapper.SysConfigMapper;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.util.Arrays;

/**
 * stations Boot 入口
 */
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
@EnableMPP
@EnableScheduling
@Slf4j
public class EntranceApplication implements ApplicationRunner {

    @Getter
    public final ConfigurableApplicationContext context;
    public final SysConfigMapper sysConfigMapper;

    public EntranceApplication(ConfigurableApplicationContext context, SysConfigMapper sysConfigMapper) {
        this.context = context;
        this.sysConfigMapper = sysConfigMapper;
    }

    public static void main(String[] args) {
        SpringApplication.run(EntranceApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) {
        try {
            SysConfig license = sysConfigMapper.selectByCode("license");
            String configValue;
            if (license != null) {
                configValue = license.getConfigValue();
                if (StrUtil.isEmpty(configValue)) {
                    log.error("系统未授权,请联系厂商进行授权,侵权必究");
                    context.close();
                    return;
                }
            } else {
                log.error("系统未授权,请联系厂商进行授权,侵权必究");
                context.close();
                return;
            }
            SystemInfoUtils.checkLicense(configValue);
        } catch (Exception e) {
            log.error("系统启动失败! {}", Arrays.toString(e.getStackTrace()));
            context.close();
        }
    }

}
